package org.jeecg.modules.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.entity.ConActivityApplyItem;
import org.jeecg.modules.activity.entity.UsrUserActivityApply;
import org.jeecg.modules.activity.mapper.ConActivityApplyItemMapper;
import org.jeecg.modules.activity.mapper.ConActivityApplyItemValueMapper;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.activity.service.IConActivityApplyItemService;
import org.jeecg.modules.activity.service.IUsrUserActivityApplyService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.task.entity.TaskInfoBase;
import org.jeecg.modules.task.entity.TaskWorkflowStep;
import org.jeecg.modules.task.service.ITaskOperationService;
import org.jeecg.modules.task.service.ITaskWorkflowStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description: 活动信息信息
* @Author: wufanxin
* @Date:   2020-06-28
* @Version: V1.0
*/
@Slf4j
@Api(tags="活动信息")
@RestController
@RequestMapping("/activity/activityInfo")
public class ActivityInfoController extends JeecgController<ActivityInfo, IActivityInfoService> {

   @Autowired
   private IActivityInfoService activityInfoService;

   @Autowired
   private IConActivityApplyItemService itemService;


    @Autowired
    private ITaskWorkflowStepService stepService;

    @Autowired
    ITaskOperationService operationService;

    @Autowired
    IUsrUserActivityApplyService applyService;


    /**
     * 内容信息列表查询
     *
     * @param activityInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "活动信息-分页列表查询")
    @ApiOperation(value="活动信息-分页列表查询", notes="活动信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ActivityInfo activityInfo,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        String contentTitle = null;
        if(!StringUtil.isEmpty(activityInfo.getActivityTitle())){
            contentTitle = activityInfo.getActivityTitle();
        }
        QueryWrapper<ActivityInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time").eq("del_flag",0).in("is_release","1","3","2");
        if(!StringUtil.isEmpty(contentTitle)){
            queryWrapper.like("activity_title",contentTitle);
        }

        Page<ActivityInfo> page = new Page<ActivityInfo>(pageNo, pageSize);
        IPage<ActivityInfo> pageList = activityInfoService.page(page, queryWrapper);
        return Result.ok(pageList);
    }




    @AutoLog(value = "活动信息-附加信息")
    @ApiOperation(value="活动信息-附加信息", notes="活动信息-附加信息")
    @GetMapping(value = "/queryActivityAddition")
    public Result<?> queryActivityAddition(@RequestParam(name="taskNo") String taskNo,HttpServletRequest req) {
        return Result.ok(itemService.getApplyInfo(taskNo));
    }




    /**
     * 我的内容信息
     *
     * @param activityInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "活动信息-我的活动信息")
    @ApiOperation(value="活动信息-我的活动信息", notes="活动信息-我的活动信息")
    @GetMapping(value = "/myActivityList")
    public Result<?> myActivityList(ActivityInfo activityInfo,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //查询这个人参与的所有步骤
        QueryWrapper<TaskWorkflowStep> stepQueryWrapper = new QueryWrapper<>();
        stepQueryWrapper.eq("deal_user_id",loginUser.getId());
        List<TaskWorkflowStep> stepList = stepService.list(stepQueryWrapper);
        List<String> inList = new ArrayList<>();
        stepList.stream().forEach(item->{
            inList.add(item.getTaskNo());
        });
        QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
        activityInfoQueryWrapper.in("task_id",inList).eq("del_flag",0).in("is_release","1","3","2").orderByDesc("create_time");
        Page<ActivityInfo> page = new Page<ActivityInfo>(pageNo, pageSize);
        IPage<ActivityInfo> pageList = activityInfoService.page(page, activityInfoQueryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 发布内容到咨询app
     *
     * @param req
     * @return
     */
    @AutoLog(value = "活动信息-发布活动到咨询app")
    @ApiOperation(value="活动信息-发布活动到咨询app", notes="活动信息-发布活动到咨询app")
    @PostMapping(value = "/publishActivity")
    public Result<?> publishActivity(@RequestBody Map<String,Object> publish,
                                    HttpServletRequest req) {
        //查询任务信息
        TaskInfoBase taskInfoBase = operationService.queryTaskInfoByTaskNo(publish.get("taskNo").toString());
        //保存发布信息
        Map<String,Object> publishInfo = (Map<String,Object>)publish.get("publishInfo");
        operationService. savePublish(taskInfoBase,publishInfo);
        return Result.ok();
    }


    /**
     *
     *用户活动报名
     * @param usrUserActivityApply
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "活动信息-用户活动报名")
    @ApiOperation(value="活动信息-用户活动报名", notes="活动信息-用户活动报名")
    @GetMapping(value = "/queryActivityApplyUser")
    public Result<?> queryActivityApplyUser(UsrUserActivityApply usrUserActivityApply,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        Map<String,Object> param = new HashMap<>();
        param.put("pageNo",pageNo == 1?0:(pageNo-1)*10);
        param.put("pageSize",pageSize);
        param.put("activityId",usrUserActivityApply.getActivityId());
        Page<Map<String,Object>> pageList = applyService.getApplyInfo(param);
        return Result.ok(pageList);
    }

}
